import { defineStore } from 'pinia'
import { useI18n } from 'vue-i18n'
import { computed } from 'vue'

export const useGlobalStore = defineStore('global', () => {
  const i18n = useI18n()

  const locale = computed({
    get: () => i18n.locale.value,
    set: (lang) => {
      i18n.locale.value = lang
      localStorage.setItem('language', lang)
    },
  })

  const changeLang = (lang) => {
    console.log('切换语言:', lang)
    locale.value = lang // ✅ locale 是响应式的
  }

  return { locale, changeLang }
})
